﻿using Gateway.Common.Extensions;
using Gateway.Domain.EFCore.Configuration;
using Gateway.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Gateway.Domain.Configuration;

public class QuestionLanguageConfiguration : EntityConfiguration<QuestionLanguage>
{
    public override void Configure(EntityTypeBuilder<QuestionLanguage> builder)
    {
        builder.ToTable(t => t.HasComment("试题配置语言表"));
        builder.Property(x => x.QuestionId).IsRequired().HasMaxLength(36).HasComment("试题标识");
        builder.Property(x => x.Language).IsRequired().HasMaxLength(100).HasComment("语言");
        builder.Property(x => x.Examples).IsRequired().HasComment("示例代码");
        builder.Property(x => x.ExamplesFileName).HasMaxLength(500).IsRequired().HasComment("示例代码的文件名称");
        builder.Property(x => x.ExecFileName).HasMaxLength(500).IsRequired().HasComment("执行代码的文件名称");
        builder.Property(x => x.DependCodes).HasMaxLength(1000)
            .HasConversion(x => string.Join(",", x ?? new List<string>()), p => p.ToSplitList(","))
            .HasComment("依赖的公共库的代码标识集合，逗号分割");
        builder.HasOne(x => x.Question).WithMany(x => x.Languages).HasForeignKey(x => x.QuestionId)
            .OnDelete(DeleteBehavior.Cascade);
        base.Configure(builder);
    }
}